package com.zqq.base.permission

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import com.zqq.base.R
import pub.devrel.easypermissions.EasyPermissions
import pub.devrel.easypermissions.PermissionRequest
import pub.devrel.easypermissions.helper.BaseSupportPermissionsHelper
import java.lang.reflect.Field


object PermissionManager {

    fun hasPermission(context: Context,vararg perms:String):Boolean{
        return EasyPermissions.hasPermissions(context,*perms)
    }


    fun hasMediaPermission(context: Context):Boolean{
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            PermissionManager.hasPermission(context, Manifest.permission.READ_MEDIA_IMAGES)
        } else {
            PermissionManager.hasPermission(
                context, Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
        }
    }


    /**
     * 申请权限
     * @param activity
     * @param reueqstCode  请求码
     * @param permissionInfo 权限描述
     * @param perms  权限
     */
    @SuppressLint("RestrictedApi")
    fun requestPermission(
        activity: Activity,
        reueqstCode: Int,
        permissionInfo: String,
        vararg perms: String
    ) {
        val request: PermissionRequest = PermissionRequest.Builder(activity, reueqstCode,*perms)
            .setRationale(permissionInfo)
            .setPositiveButtonText(activity.getString(R.string.sure))
            .setNegativeButtonText(activity.getString(R.string.cancle))
            .build()
        try {  //第一次拒绝后默认对话框样式更改   https://blog.csdn.net/leeeeeev/article/details/115940037
            val clazz: Class<*> = request.javaClass
            val field: Field = clazz.getDeclaredField("mHelper")
            field.setAccessible(true)
            val permissionHelper = field.get(request) as BaseSupportPermissionsHelper<AppCompatActivity>
            field.set(request, PermissionsHelperProxy(permissionHelper))
        } catch (e: Exception) {
        }
        EasyPermissions.requestPermissions(request)
    }

    /**
     * 存储权限
     */
    @SuppressLint("RestrictedApi")
    fun requestStorePermission(activity: Activity?, reueqstCode: Int) {
        val request = PermissionRequest.Builder(
            activity!!, reueqstCode,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
        )
            .setRationale("此功能需要您的存储权限!")
            .setPositiveButtonText("确定")
            .setNegativeButtonText("取消")
            .build()
        try {
            val clazz: Class<*> = request.javaClass
            val field: Field = clazz.getDeclaredField("mHelper")
            field.setAccessible(true)
            val permissionHelper = field.get(request) as BaseSupportPermissionsHelper<AppCompatActivity>
            field.set(request, PermissionsHelperProxy(permissionHelper))
        } catch (e: Exception) {
        }
        EasyPermissions.requestPermissions(request)
    }

    /**
     * 申请媒体权限
     */
    @SuppressLint("RestrictedApi")
    fun requestMediaPermission(activity: Activity, reueqstCode: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            val request = PermissionRequest.Builder(
                activity, reueqstCode,
                Manifest.permission.READ_MEDIA_IMAGES,
                Manifest.permission.READ_MEDIA_AUDIO, Manifest.permission.READ_MEDIA_VIDEO
            )
                .setRationale(activity.getString(R.string.permission_media))
                .setPositiveButtonText(activity.getString(R.string.sure))
                .setNegativeButtonText(activity.getString(R.string.cancle))
                .build()
            try {
                val clazz: Class<*> = request.javaClass
                val field: Field = clazz.getDeclaredField("mHelper")
                field.setAccessible(true)
                val permissionHelper = field.get(request) as BaseSupportPermissionsHelper<AppCompatActivity>
                field.set(request, PermissionsHelperProxy(permissionHelper))
            } catch (e: Exception) {
            }
            EasyPermissions.requestPermissions(request)
        } else {
            requestStorePermission(activity, reueqstCode)
        }
    }

    /**
     * 定位权限
     * @param activity
     * @param reueqstCode
     */
    @SuppressLint("RestrictedApi")
    fun requestLocationPermission(activity: Activity, reueqstCode: Int) {
        val request = PermissionRequest.Builder(
            activity, reueqstCode,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION
        )
            .setRationale(activity.getString(R.string.permission_location))
            .setPositiveButtonText(activity.getString(R.string.sure))
            .setNegativeButtonText(activity.getString(R.string.cancle))
            .build()
        try {
            val clazz: Class<*> = request.javaClass
            val field: Field = clazz.getDeclaredField("mHelper")
            field.setAccessible(true)
            val permissionHelper = field.get(request) as BaseSupportPermissionsHelper<AppCompatActivity>
            field.set(request, PermissionsHelperProxy(permissionHelper))
        } catch (e: Exception) {
        }
        EasyPermissions.requestPermissions(request)
    }

    /**
     * 录音权限
     * @param activity
     * @param reueqstCode
     */
    @SuppressLint("RestrictedApi")
    fun requestAudioPermission(activity: Activity, reueqstCode: Int) {
        val request = PermissionRequest.Builder(
            activity, reueqstCode,
            Manifest.permission.RECORD_AUDIO
        )
            .setRationale(activity.getString(R.string.permission_record))
            .setPositiveButtonText(activity.getString(R.string.sure))
            .setNegativeButtonText(activity.getString(R.string.cancle))
            .build()
        try {
            val clazz: Class<*> = request.javaClass
            val field: Field = clazz.getDeclaredField("mHelper")
            field.setAccessible(true)
            val permissionHelper = field.get(request) as BaseSupportPermissionsHelper<AppCompatActivity>
            field.set(request, PermissionsHelperProxy(permissionHelper))
        } catch (e: Exception) {
        }
        EasyPermissions.requestPermissions(request)
    }

    /**
     * 相机权限
     * @param activity
     * @param reueqstCode
     */
    @SuppressLint("RestrictedApi")
    fun requestCameraPermission(activity: Activity, reueqstCode: Int) {
        val request = PermissionRequest.Builder(
            activity, reueqstCode,
            Manifest.permission.CAMERA
        )
            .setRationale(activity.getString(R.string.permission_camera))
            .setPositiveButtonText(activity.getString(R.string.sure))
            .setNegativeButtonText(activity.getString(R.string.cancle))
            .build()
        try {
            val clazz: Class<*> = request.javaClass
            val field: Field = clazz.getDeclaredField("mHelper")
            field.setAccessible(true)
            val permissionHelper = field.get(request) as BaseSupportPermissionsHelper<AppCompatActivity>
            field.set(request, PermissionsHelperProxy(permissionHelper))
        } catch (e: Exception) {
        }
        EasyPermissions.requestPermissions(request)
    }

    /**
     * 忽略电量优化
     *
     * @param activity
     * @param reueqstCode
     */
    @SuppressLint("RestrictedApi")
    fun requestIgnorBatteryPermission(activity: Activity, reueqstCode: Int) {
        val request = PermissionRequest.Builder(
            activity, reueqstCode,
            Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
        )
            .setRationale("为了更好的接收到消息,请忽略电池优化!")
            .setPositiveButtonText(activity.getString(R.string.sure))
            .setNegativeButtonText(activity.getString(R.string.cancle))
            .build()
        try {
            val clazz: Class<*> = request.javaClass
            val field: Field = clazz.getDeclaredField("mHelper")
            field.setAccessible(true)
            val permissionHelper = field.get(request) as BaseSupportPermissionsHelper<AppCompatActivity>
            field.set(request, PermissionsHelperProxy(permissionHelper))
        } catch (e: Exception) {
        }
        EasyPermissions.requestPermissions(request)
    }


    /**
     * 允许写入配置
     *
     * @param activity
     * @param reueqstCode
     */
    @SuppressLint("RestrictedApi")
    fun requestSyncSettingsPermission(activity: Activity, reueqstCode: Int) {
        val request = PermissionRequest.Builder(
            activity, reueqstCode,
            Manifest.permission.WRITE_SYNC_SETTINGS
        )
            .setRationale("为了更好的接收到消息,请允许写入配置权限!")
            .setPositiveButtonText(activity.getString(R.string.sure))
            .setNegativeButtonText(activity.getString(R.string.cancle))
            .build()
        try {
            val clazz: Class<*> = request.javaClass
            val field: Field = clazz.getDeclaredField("mHelper")
            field.setAccessible(true)
            val permissionHelper = field.get(request) as BaseSupportPermissionsHelper<AppCompatActivity>
            field.set(request, PermissionsHelperProxy(permissionHelper))
        } catch (e: Exception) {
        }
        EasyPermissions.requestPermissions(request)
    }


    /**
     * 读取手机状态
     *
     * @param activity
     * @param reueqstCode
     */
    @SuppressLint("RestrictedApi")
    fun requestReadPhoneStatePermission(activity: Activity, reueqstCode: Int) {
        val request = PermissionRequest.Builder(
            activity, reueqstCode,
            Manifest.permission.READ_PHONE_STATE
        )
            .setRationale("为了更好的接收到消息,请允许获取手机信息权限!")
            .setPositiveButtonText(activity.getString(R.string.sure))
            .setNegativeButtonText(activity.getString(R.string.cancle))
            .build()
        try {
            val clazz: Class<*> = request.javaClass
            val field: Field = clazz.getDeclaredField("mHelper")
            field.setAccessible(true)
            val permissionHelper = field.get(request) as BaseSupportPermissionsHelper<AppCompatActivity>
            field.set(request, PermissionsHelperProxy(permissionHelper))
        } catch (e: Exception) {
        }
        EasyPermissions.requestPermissions(request)
    }

    @SuppressLint("RestrictedApi")
    fun requestBluetoothPermission(activity: Activity, reueqstCode: Int) {
        val request = PermissionRequest.Builder(
            activity, reueqstCode,
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION
        )
            .setRationale(activity.getString(R.string.permission_bluetooth))
            .setPositiveButtonText(activity.getString(R.string.sure))
            .setNegativeButtonText(activity.getString(R.string.cancle))
            .build()
        try {
            val clazz: Class<*> = request.javaClass
            val field: Field = clazz.getDeclaredField("mHelper")
            field.setAccessible(true)
            val permissionHelper = field.get(request) as BaseSupportPermissionsHelper<AppCompatActivity>
            field.set(request, PermissionsHelperProxy(permissionHelper))
        } catch (e: Exception) {
        }
        EasyPermissions.requestPermissions(request)
    }


    @SuppressLint("RestrictedApi")
    fun requestBluetoothPermission31(activity: Activity, reueqstCode: Int) {
        val request = PermissionRequest.Builder(
            activity, reueqstCode,
            Manifest.permission.BLUETOOTH_SCAN,
            Manifest.permission.BLUETOOTH_CONNECT,
            Manifest.permission.ACCESS_FINE_LOCATION
        )
            .setRationale(activity.getString(R.string.permission_bluetooth))
            .setPositiveButtonText(activity.getString(R.string.sure))
            .setNegativeButtonText(activity.getString(R.string.cancle))
            .build()
        try {
            val clazz: Class<*> = request.javaClass
            val field: Field = clazz.getDeclaredField("mHelper")
            field.setAccessible(true)
            val permissionHelper = field.get(request) as BaseSupportPermissionsHelper<AppCompatActivity>
            field.set(request, PermissionsHelperProxy(permissionHelper))
        } catch (e: Exception) {
        }
        EasyPermissions.requestPermissions(request)
    }

}